package com.example.himalayaproject.play.playalbum.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.play.playalbum.bean.albumdetial.Album;
import com.example.himalayaproject.play.playalbum.bean.albumdetial.AlbumEntity;
import com.example.himalayaproject.play.playalbum.callback.AlbumPlayHttpService;
import com.example.himalayaproject.play.playalbum.adapter.PlayerAdapter;
import com.example.himalayaproject.play.playalbum.playservice.AlbumPlayService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class PlayActivity extends AppCompatActivity implements Callback<Album>, View.OnClickListener ,ServiceConnection{

    private ImageView backBar;
    private TextView titleTextView;
    private ImageView moreBar;
    private RecyclerView recycler;
    private AlbumPlayHttpService service;
    private PlayerAdapter adapter;
    private List<AlbumEntity> albumEntities;
    private AlbumPlayService playService;
    private String playUrl32;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);

        Intent intent = getIntent();
        String trackId = intent.getStringExtra("trackId");
        String trackUid = intent.getStringExtra("trackUid");// 实体类中的uid
        String playTimes = intent.getStringExtra("playTimes");
        String tracks = intent.getStringExtra("tracks");
        String intro = intent.getStringExtra("intro");
        String tags = intent.getStringExtra("tags");

        aboutView();


        service = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com//")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(AlbumPlayHttpService.class);
        service.getAlbumEntity(trackId,trackUid).enqueue(this);

        aboutPlayService();

        adapter = new PlayerAdapter(this,playTimes,tracks,intro,tags);
        recycler.setAdapter(adapter);
        backBar.setOnClickListener(this);
        moreBar.setOnClickListener(this);
        titleTextView.setText(albumEntities.get(0).getTitle());


    }

    private void aboutPlayService() {
        if (playService == null) {
            // 第三步，绑定Service
            Intent intent = new Intent(this, AlbumPlayService.class);
            bindService(intent,this, Context.BIND_AUTO_CREATE);
            intent.putExtra("playUrl32",playUrl32);
            startService(intent);
        } else {
            playService.start();
        }
    }

    private void aboutView() {
        albumEntities = new ArrayList<>();
        backBar = (ImageView) findViewById(R.id.player_bar_back);
        titleTextView = (TextView) findViewById(R.id.player_title);
        moreBar = (ImageView) findViewById(R.id.player_bar_more);
        recycler = (RecyclerView) findViewById(R.id.player_recycler);
    }

    @Override
    public void onResponse(Call<Album> call, Response<Album> response) {
        playUrl32 = response.body().getAlbumEntity().getPlayUrl32();
        albumEntities.add(response.body().getAlbumEntity());
        adapter.addAll(albumEntities);
    }

    @Override
    public void onFailure(Call<Album> call, Throwable t) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.player_bar_back:
                break;
            case R.id.player_bar_more:
                break;
        }
    }

    // 第四步，判断绑定是否成功
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        AlbumPlayService.AlbumBinder albumBinder = (AlbumPlayService.AlbumBinder) service;
        // 第五步，通过胶水间接获取到Service对象
        playService = albumBinder.getAlbumPlayService();
        playService.start();
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
}
